package com.gxpt.app.view.myview.cityview;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gxpt.app.AppUtils;
import com.gxpt.app.BaseActivity;
import com.gxpt.app.Constant;
import com.gxpt.app.R;
import com.gxpt.app.bean.normal;
import com.utils.T;
import com.utils.logger.Logger;
import com.utils.okhttputils.HttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import net.sourceforge.pinyin4j.PinyinHelper;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import okhttp3.Call;

/**
 * Created by dufeng on 2017/8/4.
 */

@ContentView(R.layout.activity_city_select)
public class Activity_city_select extends BaseActivity {

    @ViewInject(R.id.toback)
    Button toback;

    @ViewInject(R.id.txt_title)
    TextView txt_title;

    @ViewInject(R.id.my_letterview)
    MyLetterView myLetterView;
//
    @ViewInject(R.id.tv_dialog)
    TextView tvDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
//        setContentView(R.layout.activity_set_pwd);
        initView();
        initData();
    }

    @Override
    public void initView() {
//        myLetterView=(MyLetterView) findViewById(R.id.my_letterview);
//        tvDialog =(TextView) findViewById(R.id.tv_dialog);
//
//        toback =(Button) findViewById(R.id.toback);
//        txt_title=(TextView) findViewById(R.id.txt_title);

        txt_title.setText(getString(R.string.txt_city_select));

        //将中间展示字母的TextView传递到myLetterView中并在其中控制它的显示与隐藏
        myLetterView.setTextView(tvDialog);
        //注册MyLetterView中监听(跟setOnClickListener这种系统默认写好的监听一样只不过这里是我们自己写的)
        myLetterView.setOnSlidingListener(new MyLetterView.OnSlidingListener() {

            @Override
            public void sliding(String str) {
                tvDialog.setText(str);
            }
        });

        String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray('行');
        for (int i = 0; i < pinyinArray.length; ++i)
        {
            Logger.d(pinyinArray[i]);
        }
    }

    @Override
    public void initData() {
        toNet_getregion();
    }


    @Event(value={R.id.toback,R.id.tofinish})
    private void getEvent(View view){
        switch(view.getId()){
            case R.id.toback:
                closeActivity();
                break;
        }
    }

    //////NET DO
    StringCallback do_getregion = new StringCallback(){


        @Override
        public void onError(Call call, Exception e, int i) {
            Logger.e(e.getMessage());
        }

        @Override
        public void onResponse(String s, int i) {
            if(hud.isShowing()){
                hud.dismiss();
            }
            Logger.e(s);
            try{
                normal data = new Gson().fromJson(s, normal.class);

                if(data.state !=1 ){
                    T.show(context,data.msg,0);
                }else{
                    T.show(context,getString(R.string.alert_success_reg),1);
                    closeOtherActivity("Activity_reg");
                    closeActivity();
                }
            }catch (Exception e){
                e.printStackTrace();
                T.show(context,getResources().getString(R.string.alert_again),0);
            }
        }
    };

    StringCallback do_forgetpwd = new StringCallback(){


        @Override
        public void onError(Call call, Exception e, int i) {
            Logger.e(e.getMessage());
        }

        @Override
        public void onResponse(String s, int i) {
            if(hud.isShowing()){
                hud.dismiss();
            }
            Logger.e(s);
            try{

                normal data = new Gson().fromJson(s, normal.class);

                if(data.state !=1 ){
                    T.show(context,data.msg,0);
                }else{
                    T.show(context,getString(R.string.alert_success_setpwd),1);
                    closeOtherActivity("Activity_reg");
                    closeActivity();
                }
            }catch (Exception e){
                e.printStackTrace();
                T.show(context,getResources().getString(R.string.alert_again),0);
            }
        }
    };
    /////NET
    private void toNet_getregion(){
        hud.show();
        JSONObject jsonBody=new JSONObject();
        try {
            jsonBody.put("device_number",         AppUtils.get_device_id(context));
            jsonBody.put("device_type",           Constant.device_type);
            jsonBody.put("token",                 AppUtils.get_token(context));
            jsonBody.put("pageName",             "category");
            jsonBody.put("serviceName",          "getregion");

            jsonBody.put("sub_id",                "");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        Logger.i(jsonBody.toString()+" ==["+Constant.URL_register+" ]");
        HttpUtils.http_post_string(Constant.URL_getregion,jsonBody.toString(),do_getregion);
    }

    private void toNet_forgetpwd(){
        hud.show();
        JSONObject jsonBody=new JSONObject();
        try {
            jsonBody.put("device_number",         AppUtils.get_device_id(context));
            jsonBody.put("device_type",           Constant.device_type);
            jsonBody.put("token",                 AppUtils.get_token(context));
            jsonBody.put("pageName",             "member");
            jsonBody.put("serviceName",          "forgetpwd");

//            jsonBody.put("mem_tel",          mem_tel);
//            jsonBody.put("smsyzm",           smsyzm);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Logger.i(jsonBody.toString()+" ==["+Constant.URL_forgetpwd+" ]");
        HttpUtils.http_post_string(Constant.URL_forgetpwd,jsonBody.toString(),do_forgetpwd);
    }


























}
